#!/usr/bin/env python3

import os
import sys

import ff

TGTBR_EV = 'MCLEAN_FF_BITRATE'
PRESET_EV = 'MCLEAN_FF_PRESET'
DRYRUN_EV = 'MCLEAN_DRYRUN'

def main(argv):
    src = os.path.expanduser(argv[1])
    dest = os.path.expanduser(argv[2])

    target_br = 1000
    preset = 'veryfast'
    dr = False

    if TGTBR_EV in os.environ:
        target_br = int(os.environ[TGTBR_EV])

    if PRESET_EV in os.environ:
        preset = os.environ[PRESET_EV]

    if DRYRUN_EV in os.environ:
        dr = bool(os.environ[DRYRUN_EV])

    probe = ff.probe_file(src)
    props = ff.extract_stream_props(probe)
    exp_size = (props['duration_sec'] * target_br) // 8 // 1024 # TODO adj for audio

    print('SRC CODEC:', props['codec'])
    print('INPUT HEIGHT:', props['res_h'])
    print('TARGET BITRATE:', target_br)
    print('EXP OUTPUT:', exp_size, 'MiB')

    yn = input('Continue? y/n: ')
    if yn != 'y':
        return 1

    ff.reencode(src, dest, target_br, preset=preset, dryrun=dr)
    print('WRITTEN OK')
    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv))
